package com.caidanxiaoyuan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caidanxiaoyuan.model.HuoDongBaoMingList;
import com.caidanxiaoyuan.model.HuoDongBaoMingListView;
import com.caidanxiaoyuan.model.HuoDongList;
import com.caidanxiaoyuan.model.HuoDongLiuYanList;
import com.caidanxiaoyuan.model.HuoDongLiuYanListView;
import com.caidanxiaoyuan.model.HuodongAdvertList;
import com.caidanxiaoyuan.model.HuodongListView;
import com.caidanxiaoyuan.model.ShopList;
import com.caidanxiaoyuan.model.ShopListView;
import com.caidanxiaoyuan.model.ShopLiuyanListView;
import com.caidanxiaoyuan.service.HuoDongAdverListService;
import com.caidanxiaoyuan.service.HuoDongBaoMingListService;
import com.caidanxiaoyuan.service.HuoDongBaoMingListViewService;
import com.caidanxiaoyuan.service.HuoDongListService;
import com.caidanxiaoyuan.service.HuoDongListViewService;
import com.caidanxiaoyuan.service.HuoDongLiuYanListService;
import com.caidanxiaoyuan.service.HuoDongLiuYanListViewService;
import com.caidanxiaoyuan.util.JsonUtil;
import com.caidanxiaoyuan.util.ResponseUtil;
import com.github.pagehelper.Page;

@Controller
@CrossOrigin
@RequestMapping("/huodong")
public class HuodongController {
	
	@Autowired
	private HuoDongListViewService huoDongListViewService;
	@Autowired
	private HuoDongAdverListService huoDongAdverListService;
	@Autowired
	private HuoDongListService huoDongListService;
	@Autowired
	private HuoDongBaoMingListService huoDongBaoMingListService;
	@Autowired
	private HuoDongBaoMingListViewService huoDongBaoMingListViewService;
	@Autowired
	private HuoDongLiuYanListViewService huoDongLiuYanListViewService;
	@Autowired
	private HuoDongLiuYanListService huoDongLiuYanListService;
	
	/**
	 * 
	 * @Description : 活动列表
	 * @author zealot
	 * @param @param page
	 * @param @param t
	 * @param @return
	 * @return Map<String,Object>
	 * @version 创建时间 : 2017年2月21日 下午5:04:35
	 * @thorws
	 */
	@RequestMapping("/list")
	@ResponseBody
	public  Map<String, Object> huoDongListView(Page<HuodongListView> page,HuodongListView t){
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HuodongListView> list = (Page<HuodongListView>) huoDongListViewService.selectAll(page,t);
		map.put("data",list.getResult());
		map.put("totalpage", list.getPages());
		map.put("totalrows", list.getTotal());
		map.put("time", String.valueOf(System.currentTimeMillis()).toString().substring(0,10));
		return map;
	}
	
	/**
	 * 
	 * @Description : 活动首页
	 * @author zealot
	 * @param @param page
	 * @param @param response
	 * @return void
	 * @version 创建时间 : 2017年2月21日 下午5:04:47
	 * @thorws
	 */
	@RequestMapping("/advert/list")
	public void advertList(Page<HuodongAdvertList> page,HttpServletResponse response){
		ResponseUtil.result(response, JsonUtil.toJsonStr(huoDongAdverListService.selectAll(page)));
	}
	
	/**
	 * 
	* @Description: 留言编辑添加  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年3月1日 上午11:43:36  
	* @throws
	 */
	@RequestMapping("/edit")
	public void huodongEdit(HuoDongList e,HttpServletResponse response) throws Exception{
		switch(e.getAction())
	    {
	        case "append":
	        	e.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
	        	huoDongListService.insert(e);
	            break;
	        case "edit":
	        	huoDongListService.updateByPrimaryKey(e);
	            break;
	    }
		ResponseUtil.write(response, 12);
	}
	
	/**
	 * 
	* @Description: 彩活动详情页
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 下午4:29:25  
	* @throws
	 */
	@RequestMapping("/view")
	public void view(HuodongListView e,HttpServletResponse response){
		e = huoDongListViewService.selectByPrimaryKey(e.getId());
		HuoDongBaoMingList t = new HuoDongBaoMingList();
		t.setHuodongid(e.getId());
		t.setMyid(e.getMyid());
		List<HuoDongBaoMingList> nt = huoDongBaoMingListService.selectByUs(t);
		if(nt!=null){
			if(nt.size()>0){
				e.setIsbaoming(nt.get(0));
			}
		}
		ResponseUtil.result(response, JsonUtil.toJsonStr(e));
	}
	
	/**
	 * 
	* @Description: 活动详情H5  
	* @author zealot
	* @param @param e
	* @param @param request
	* @param @return 
	* @return String    
	* @version 创建时间：2017年3月1日 下午5:08:46  
	* @throws
	 */
	@RequestMapping("/h5/huodong")
	public String shenghuo(HuodongListView e,HttpServletRequest request){
		e = huoDongListViewService.selectByPrimaryKey(e.getId());
		request.setAttribute("e", e);
		
		HuoDongBaoMingListView n1 = new HuoDongBaoMingListView();
		n1.setHuodongid(e.getId());
		request.setAttribute("n", huoDongBaoMingListViewService.selectByUs(n1));
		
		HuoDongLiuYanListView n2 = new HuoDongLiuYanListView();
		n2.setHuodongid(e.getId());
		request.setAttribute("f",huoDongLiuYanListViewService.selectByUs(n2));
		return "h5/huodong";
	}
	
	/**
	 * @throws Exception 
	 * 
	* @Description: 报名  
	* @author zealot
	* @param @param t
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 下午5:42:53  
	* @throws
	 */
	@RequestMapping("/baoming/edit")
	public void baomingEdit(HuoDongBaoMingList t,HttpServletResponse response) throws Exception{
		t.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
		HuoDongList e = new HuoDongList();
		huoDongBaoMingListService.insert(t);
		t = huoDongBaoMingListService.count(t);
		e.setBaomingshu(Short.valueOf(t.getTotalrows().toString()));
		e.setId(t.getHuodongid());
		huoDongListService.updateByPrimaryKey(e);
		ResponseUtil.write(response, 12);
	}
	
	/**
	 * 
	* @Description: 活动报名列表  
	* @author zealot
	* @param @param t
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 下午5:45:55  
	* @throws
	 */
	@RequestMapping("/baoming/list")
	public void baomingList(HuoDongBaoMingListView t,HttpServletResponse response){
		ResponseUtil.result(response, JsonUtil.toJsonStr(huoDongBaoMingListViewService.selectByUs(t)));
	}
	
	/**
	 * 
	* @Description: 活动留言  
	* @author zealot
	* @param @param t
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 下午5:47:18  
	* @throws
	 */
	@RequestMapping("/liuyan/list")
	public void liuyanList(Page<HuoDongLiuYanListView> page,HuoDongLiuYanListView t,HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HuoDongLiuYanListView> list = (Page<HuoDongLiuYanListView>) huoDongLiuYanListViewService.selectAll(page, t);
		map.put("data", list.getResult());
		map.put("totalpage", list.getPages());
		map.put("totalrows", list.getTotal());
		map.put("time", String.valueOf(System.currentTimeMillis()).toString().substring(0,10));
		ResponseUtil.result(response, JsonUtil.toJsonStr(map));
	}
	
	/**
	 * 
	* @Description: 活动留言添加  
	* @author zealot
	* @param @param t
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年3月1日 下午6:10:38  
	* @throws
	 */
	@RequestMapping("/liuyan/edit")
	public void liuyanEdit(HuoDongLiuYanList t,HttpServletResponse response) throws Exception{
		t.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
		huoDongLiuYanListService.insert(t);
		ResponseUtil.write(response, 12);
	}
	
	/**
	 * 
	* @Description: 删除活动  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年3月2日 上午9:40:28  
	* @throws
	 */
	@RequestMapping("/delete")
	public void delete(HuoDongList e,HttpServletResponse response) throws Exception{
		huoDongListService.deleteByPrimaryKey(e.getId());
		ResponseUtil.write(response, 12);
	}
}
